package tech.gismap.admin.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import tech.gismap.admin.domain.DaTable;
import tech.gismap.admin.web.bo.DaTableBo;
import tech.gismap.admin.web.vo.DaTableVo;
import tech.gismap.admin.mapper.DaTableMapper;
import tech.gismap.common.mybatis.core.page.PageQuery;
import tech.gismap.common.mybatis.core.page.TableDataInfo;

/**
* 数据集定义
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class DaTableService {

    private final DaTableMapper baseMapper;

    public TableDataInfo<DaTableVo> selectPageList(DaTableBo bo, PageQuery pageQuery) {
        Page<DaTableVo> page = baseMapper.selectVoPage(pageQuery.build(), this.buildQueryWrapper(bo));
        return TableDataInfo.build(page);
    }

    private Wrapper<DaTable> buildQueryWrapper(DaTableBo bo) {
        LambdaQueryWrapper<DaTable> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(bo.getId()!=null,DaTable::getId,bo.getId());
        wrapper.eq(bo.getCreateBy()!=null,DaTable::getCreateBy,bo.getCreateBy());
        wrapper.ge(bo.getBeginCreateTime()!=null,DaTable::getCreateTime,bo.getBeginCreateTime());
        wrapper.lt(bo.getEndCreateTime()!=null,DaTable::getCreateTime,bo.getEndCreateTime());
        wrapper.eq(bo.getUpdateBy()!=null,DaTable::getUpdateBy,bo.getUpdateBy());
        wrapper.ge(bo.getBeginUpdateTime()!=null,DaTable::getUpdateTime,bo.getBeginUpdateTime());
        wrapper.lt(bo.getEndUpdateTime()!=null,DaTable::getUpdateTime,bo.getEndUpdateTime());
        wrapper.eq(bo.getCreateDept()!=null,DaTable::getCreateDept,bo.getCreateDept());
        wrapper.orderByDesc(DaTable::getUpdateTime);
        return wrapper;
    }
}
